var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var user = require('../../utils/user.js');
import * as echarts from '../../utils/ec-canvas/echarts.js';

const app = getApp();

function initChart(canvas, width, height, ge, zong) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: 200
  });
  canvas.setChart(chart);

  var option = {
    backgroundColor: "#ffffff",
    color: ["#5AA250", "#FF7F27", "#67E0E3", "#91F2DE", "#FFDB5C", "#FF9F7F"],
    series: [{
      label: {
        normal: {
          fontSize: 14
        }
      },
      type: 'pie',
      center: ['50%', '50%'],
      radius: [0, '60%'],
      data: [{
        value: zong,
        name: '商户订单总数' + zong
      }, {
        value: ge,
        name: '你的订单数' + ge
      }, ],
      itemStyle: {
        emphasis: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 2, 2, 0.3)'
        }
      }
    }]
  };

  chart.setOption(option);
  return chart;
}



Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {},
    info: {
      orderRanking: 0,
      orderTotal: 0,
      todayIncome: 0,
      todayRatio: 0,
      todaySum: 0,
      totalIncome: 0,
      useOrderTotal: 0
    },
    ind: {},
    shop: {},
    today: 0,
    yesterday: 0,
    monthe: 0
  },
  getzt() {
    let that = this
    util.request(api.ShowOrderInfo + "/1", {}, 'get', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log("sadsadad" + res.data.info.length)
      if (res.code == 200) {
        that.setData({
          today: res.data.info.length
        })
      }
      if (res.code != 200) {
        wx.showModal({
          title: '错误提示',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },
  getzt1() {
    let that = this
    util.request(api.ShowOrderInfo + "/2", {}, 'get', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log("sadsadad" + res.data.info.length)
      if (res.code == 200) {
        that.setData({
          yesterday: res.data.info.length
        })
      }
      if (res.code != 200) {
        wx.showModal({
          title: '错误提示',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },

  getzt2() {
    let that = this
    util.request(api.ShowOrderInfo + "/3", {}, 'get', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log("sadsadad" + res.data.info.length)
      if (res.code == 200) {
        that.setData({
          monthe: res.data.info.length
        })
      }
      if (res.code != 200) {
        wx.showModal({
          title: '错误提示',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },
  getorder() {

    let that = this
    util.request(api.QueryOrdersInfo, {

    }, 'GET', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'Content-Type': 'application/json;charset=UTF-8',
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          info: res.data
        })
      }
      if (res.code != 200) {
        wx.showModal({
          title: '错误提示',
          content: res.msg,
          showCancel: false
        })
      }
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("1111" + app.globalData.info)
    this.setData({
      ind: app.globalData.ind,
      shop: app.globalData.ind
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  echartInit(e) {
    console.log(e)
    console.log(app.globalData.info)
    initChart(e.detail.canvas, e.detail.width, e.detail.height, app.globalData.info.todaySum, app.globalData.info.orderTotal);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    user.checkLogin().then(res => {
      this.getzt()
      this.getzt1()
      this.getzt2()
      this.getorder()
    }).catch(() => {
      wx.clearStorageSync("token")
      wx.showModal({
        title: '温馨提示',
        content: '登录过期或未登录',
        showCancel: false,
        success: res => {
          wx.redirectTo({
            url: '/pages/login/login',
          })
        }
      })
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})